<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Api\BaseController;
use App\Wen\Utils\Settings;
use Closure;
use Illuminate\Http\Request;
use App\Lib\JwtAuth;

class AccessTokenMate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $access_token = $request->get('access_token');

        global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_IP__,$__MINI_GLOBAL_PLATFORM__,$__MINI_GLOBAL_SCENE__,$__MINI_GLOBAL_FRONT_VERSION__;
        if($__MINI_GLOBAL_IP__ == ''){
            if(_empty_($request->header('X-Forwarded-For',''))){
                $__MINI_GLOBAL_IP__ = $request->ip();
            }else{
                $__MINI_GLOBAL_IP__ = $request->header('X-Forwarded-For','');
            }
        }
        $__MINI_GLOBAL_DEVICE__ = $request->header('device','mp');
        $__MINI_GLOBAL_PLATFORM__ = $request->header('platform','android');
        $__MINI_GLOBAL_SCENE__ = $request->header('scene', 0);
        $__MINI_GLOBAL_FRONT_VERSION__ = $request->header('version', '1.2.2.16');

        if($access_token == md5('Mr.WenShao（'.get_mini_domain().'）')){

        }else{
            if(_empty_($access_token)){
                $baseController = new BaseController();
                return $baseController->fail(200001, [], 'url未携带access_token参数或access_token为空');
            }
            $mini_access_token = Settings::get('mini_access_token', '');
            if(_empty_($mini_access_token)){
                $baseController = new BaseController();
                return $baseController->fail(200008, [], '后台未配置access_token，请至后台[全局配置-安全配置-三方服务]进行配置');
            }
            if($access_token != $mini_access_token){
                $baseController = new BaseController();
                return $baseController->fail(200004, [], 'access_token参数匹配错误');
            }
        }

        return $next($request);
    }
}
